package game.UIComponent
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	import game.common.CustomBitmap;
	import game.managers.StageProxy;
	import game.utils.Utils;

	public class NumberStep extends UIBase
	{
		private var _numberTI:TextField;
		private var _upBtn:Button;
		private var _downBtn:Button;
		
		private var _bg:CustomBitmap;
		private var _maxNum:int;

		public function NumberStep(changeEvent:Function, maxNum:int, bg:String,wid:int=100)
		{
			_changeEvent=changeEvent;

			_downBtn=new Button('', 'leftnextBtn');
			_downBtn.x=-10;
			_downBtn.y=0;
			//_upBtn.height = 14;
			_downBtn.addEventListener(MouseEvent.CLICK, down);
			addChild(_downBtn);

			_bg=new CustomBitmap();
			_bg.source=bg;
			_bg.x=_downBtn.width;
			addChild(_bg);
			
			_numberTI=Utils.getCommonTextField("1",22,84,0,0x000000,true);
			_numberTI.type=TextFieldType.INPUT;
			_numberTI.width=58;
			_numberTI.restrict="0-9";
			_numberTI.maxChars=4;
			_numberTI.addEventListener(Event.CHANGE, change);
			addChild(_numberTI);
			
			
			_upBtn=new Button('', 'rightnextBtn');
			_upBtn.x=_bg.width +_bg.x + 10;
			_upBtn.y=0;
			_upBtn.addEventListener(MouseEvent.CLICK, add);
			addChild(_upBtn);
		
//			addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent):void
//				{
//					e.stopPropagation();
//				});
			this.maxNum=maxNum;
		}

		private var _changeEvent:Function;

		private function change(e:Event):void
		{
			if (int(_numberTI.text) >= maxNum){
				_upBtn.enabled = false;		
				_numberTI.text=maxNum.toString();
			}
			if (_numberTI.text == '' || int(_numberTI.text) == 0)
			{
				_numberTI.text='0';
				_numberTI.setSelection(0, 1);
				_downBtn.enabled = false;
			}
			if (_changeEvent != null){
				_changeEvent(int(_numberTI.text));
			}
		}

		public function set maxNum(value:int):void
		{
			_maxNum=value;
			//      _numberTI.maxChars=_maxNum.toString().length
		}

		public function get maxNum():int
		{
			return _maxNum;
		}

		private function add(e:MouseEvent):void
		{
			var num:int=int(_numberTI.text);
			if (num < _maxNum)
			{
				_downBtn.enabled = true;
				num++;
				_numberTI.text=num.toString();
				_numberTI.dispatchEvent(new Event(Event.CHANGE));
			}
			else
				_upBtn.enabled = false;
		}

		private function down(e:MouseEvent):void
		{
			var num:int=int(_numberTI.text);
			if (num > 0)
			{
				_upBtn.enabled = true;
				num--;
				_numberTI.text=num.toString();
				_numberTI.dispatchEvent(new Event(Event.CHANGE));
			}
			else
				_downBtn.enabled = false;
		}

		public function set value(value:int):void
		{
			_numberTI.text=value.toString();
		}

		public function get value():int
		{
			return int(_numberTI.text);
		}

	}
}